package com.zmn.oms.services.impl.warranty;

import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.model.entity.order.Order;
import com.zmn.oms.model.entity.warranty.OrderWarranty;
import com.zmn.oms.persistence.interfaces.warranty.OrderWarrantyDao;
import com.zmn.oms.services.interfaces.warranty.OrderWarrantyProductService;
import com.zmn.oms.services.interfaces.warranty.OrderWarrantyService;
import io.shardingsphere.api.HintManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 类描述： 保单service 实现
 *
 * @author lujia
 * @date 2018/10/31 15:33
 */
@Service
public class WarrantyServiceImpl implements OrderWarrantyService {

	@Resource
	private OrderWarrantyDao orderWarrantyDao;
	@Resource
	private OrderWarrantyProductService orderWarrantyProductService;

	/**
	 * 多订单id，查询保修卡
	 *
	 * @param orderIdList
	 * @return
	 */
	@Override
	public List<OrderWarranty> listOrderWarrantyCodeByOrderIdList(List<Long> orderIdList) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderWarrantyDao.listOrderWarrantyCodeByOrderIdList(orderIdList);
		}
	}

	@Override
	public List<OrderWarranty> listOrderWarrantyByOrderIdList(List<Long> orderIdList) {
		return orderWarrantyDao.listOrderWarrantyByOrderIdList(orderIdList);
	}

	/**
	 * 用户id，查询保修卡
	 * @param userId
	 * @return
	 */
	@Override
	public List<OrderWarranty> listByUserId(Long userId) {
		return orderWarrantyDao.listByUserId(userId);
	}

	/**
	 * 订单主键id查询主库
	 * @param orderId 订单id
	 * @return
	 */
	@Override
	public OrderWarranty findByOrderIdByKeySrcMaster(Long orderId) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return findByOrderId(orderId);
		}
	}

	/**
	 * 订单主键id查询
	 * @param orderId 订单id
	 * @return
	 */
	@Override
	public OrderWarranty findByOrderId(Long orderId) {
		return orderWarrantyDao.selectByPrimaryKey(orderId);
	}

	/**
	 * 新增保修卡
	 * @param warranty 保修卡
	 * @return
	 */
	@Override
	public Integer insertWarranty(OrderWarranty warranty) {
		return orderWarrantyDao.insert(warranty);
	}

	/**
	 * 激活保修卡（更新可用状态，失效时间）
	 * @param orderId
	 * @return
	 */
	@Override
	public Integer activeWarranty(Long orderId) {
		OrderWarranty orderWarranty = new OrderWarranty();
		orderWarranty.setWarrId(orderId);
		orderWarranty.setStatus(OrderConsts.ORDER_WARRANTY_STATUS_VALID);
		orderWarranty.setEffectiveTime(new Date());
		return orderWarrantyDao.updateByPrimaryKeySelective(orderWarranty);
	}

	/**
	 * 更新保修卡状态
	 * @param orderId
	 * @param status
	 * @return
	 */
	@Override
	public Integer updateWarrantyStatus(Long orderId, Integer status) {
		OrderWarranty orderWarranty = new OrderWarranty();
		orderWarranty.setWarrId(orderId);
		orderWarranty.setType(OrderConsts.ORDER_WARRANTY_TYPE_CARD);
		orderWarranty.setStatus(status);
		return orderWarrantyDao.updateWarrantyStatus(orderWarranty);
	}

	/**
	 * 更新保修卡类型
	 * @param orderId
	 * @param type
	 * @return
	 */
	@Override
	public Integer updateWarrantyType(Long orderId, Integer type) {
		OrderWarranty orderWarranty = new OrderWarranty();
		orderWarranty.setWarrId(orderId);
		orderWarranty.setType(type);
		return orderWarrantyDao.updateByPrimaryKeySelective(orderWarranty);
	}

    /**
	 * 更新保修卡类型
	 * @param orderId
	 * @param userId
	 * @return
	 */
	@Override
	public Integer takeWarranty(Long orderId, Long userId) {
		OrderWarranty orderWarranty = new OrderWarranty();
		orderWarranty.setWarrId(orderId);
		orderWarranty.setUserId(userId);
		orderWarranty.setGrantTime(new Date());
		return orderWarrantyDao.updateByPrimaryKeySelective(orderWarranty);
	}

	/**
	 * 删除保修卡
	 * @param orderId
	 * @return
	 */
	@Override
	public Integer deleteByOrderId(Long orderId) {
		orderWarrantyProductService.deleteByOrderId(orderId);
		return orderWarrantyDao.deleteByPrimaryKey(orderId);
	}

}
